package com.yangyongchao.dailyassistant.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yangyongchao.dailyassistant.R;
import com.yangyongchao.dailyassistant.utils.JsonRootBean;

import static com.yangyongchao.dailyassistant.utils.GetJsonFronHttp.getBytesFromHttp;

/**
 * Created by 杨永超的电脑 on 2017/9/6.
 */

public class EmsActivity extends AppCompatActivity {

    private TextView showTextView; //最后显示的内容
    private String companyNameFlag; //公司对应的代号
    private Button ensure;
    private Button runButton;   //查询按钮
    private EditText oddEexpress;   //快递单号输入框
    private Spinner companySpinner; //快递公司选择下拉才当
    private String companyName; //从下拉菜单获取的快递公司名字
    private String EexpressNumbel; //从快递当好输入框获得的快递单号
    String json =null;
    String dates="";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.ems_activity);
        /*
        初始化
         */
        showTextView = (TextView) findViewById(R.id.show);
        runButton = (Button) findViewById(R.id.run);
        oddEexpress = (EditText) findViewById(R.id.express_odd);
        showTextView = (TextView) findViewById(R.id.show);
        ensure = (Button) findViewById(R.id.ensunre);
        companySpinner = (Spinner) findViewById(R.id.companyspingarr);
        showTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

        /*确定 Button监听*/
        ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dates="";
                //获取快递公司
                companyName = companySpinner.getSelectedItem().toString();//获取用户选择的快递公司
                companyNameFlag = getCompanyNameFromView(companyName);//将公司名称转化为接口能使用的代号
                //获取快递编号
                EexpressNumbel = oddEexpress.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String url = "http://www.kuaidi100.com/query?type="+companyNameFlag+"&postid="+EexpressNumbel;
                        json = getBytesFromHttp(url);
                        Gson gson = new Gson();
                        int flag = 0;
                        JsonRootBean jsonRootBean = gson.fromJson(json,JsonRootBean.class);
                        try {
                            String isHsa = jsonRootBean.getData().get(flag).getTime();
                            while (isHsa!=null){
                                flag++;
                                dates += "时间   #^_^#："+jsonRootBean.getData().get(flag).getTime()
                                        +"\n信息   ╮(￣▽￣)╭\n"+jsonRootBean.getData().get(flag).getContext()+"\n\n";
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                }).start();
            }
        });

        runButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dates.equals("")){
                    dates="…(⊙_⊙)没有找到该信息…(⊙_⊙)";
                }
                showTextView.setText(dates);
            }
        });

    }

    /**
     *
     * @param c Spinner下拉菜单中读取的内荣
     * @return 接口所对应的快递公司代号
     */
    private static String getCompanyNameFromView(String c){
        String company = null;
        switch (c){
            case"申通":
                company = "shengtong";
                break;
            case"EMS":
                company = "ems";
                break;
            case"顺丰":
                company = "shunfeng";
                break;
            case"圆通":
                company = "yuantong";
                break;
            case"中通":
                company = "zhongtong";
                break;
            case"韵达":
                company = "yuhda";
                break;
            case"天天":
                company = "tiantian";
                break;
            case"汇通":
                company = "huitongkuaidi";
                break;
            case"全峰":
                company = "quanfengwuliu";
                break;
            case"德邦":
                company = "debangwuliu";
                break;
            case"宅急送":
                company = "zhaijisong";
                break;
        }
        return company;
    }
}
